package com.yuuzy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yuuzy.context.BaseContext;
import com.yuuzy.dto.PatientBasicDTO;
import com.yuuzy.dto.PatientPageQueryDTO;
import com.yuuzy.entity.Patient;
import com.yuuzy.mapper.*;
import com.yuuzy.result.PageResult;
import com.yuuzy.service.ImageService;
import com.yuuzy.service.PatientService;
import com.yuuzy.vo.MonthlyDataVO;
import com.yuuzy.vo.PatientPageQueryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class PatientServiceImpl implements PatientService {
	@Autowired
	private PatientMapper patientMapper;
	@Autowired
	private PatientBasicInfoMapper patientBasicInfoMapper;
	@Autowired
	private PatientDetailInfoMapper patientDetailInfoMapper;
	@Autowired
	private PatientImageInfoMapper patientImageInfoMapper;
	@Autowired
	private PatientReturnVisitMapper patientReturnVisitMapper;
	@Autowired
	private ImageService imageService;

	/**
	 * 新增患者
	 *
	 * @param patientBasicDTO
	 */
	@Override
	//新增患者时要同时操作多个表，使用相同的uid
	@Transactional
	public void save(PatientBasicDTO patientBasicDTO) {
		//创建patient实体，给uid和问诊人赋值
		Patient patient = new Patient();
		BeanUtils.copyProperties(patientBasicDTO, patient);
		patient.setInquirer(BaseContext.getCurrentUsername());
		String uid = patient.getTreatmentDate() + patient.getPhone();
		patient.setUid(uid);
		//为四个表创建表项
		patientBasicInfoMapper.insertPatientBasic(patient);
		patientDetailInfoMapper.insertPatientDetail(patient);
		patientImageInfoMapper.insertPatientImage(patient);
		patientReturnVisitMapper.insertPatientVisit(patient);
	}

	/**
	 * 患者分页查询
	 *
	 * @param patientPageQueryDTO
	 * @return
	 */
	@Override
	public PageResult pageQuery(PatientPageQueryDTO patientPageQueryDTO) {
		PageHelper.startPage(patientPageQueryDTO.getPage(), patientPageQueryDTO.getPageSize());
		Page<PatientPageQueryVO> page = patientBasicInfoMapper.pageQuery(patientPageQueryDTO);
		return new PageResult(page.getTotal(), page.getResult());
	}


	/**
	 * 患者批量删除
	 *
	 * @param ids
	 */
	@Override
	//同时删除四个表项
	@Transactional
	public void deleteBatch(List<Integer> ids) {
		// 删除云端保存的图片
		for (Integer id : ids) {
			imageService.delete(id);
		}
		
		//同时批量删除四个表
		patientBasicInfoMapper.deletePatientBasicByIds(ids);
		patientDetailInfoMapper.deletePatientDetailByIds(ids);
		patientImageInfoMapper.deletePatientImageByIds(ids);
		patientReturnVisitMapper.deletePatientVisitByIds(ids);

	}

	/**
	 * 根据id查找患者
	 *
	 * @param id
	 * @return
	 */
	@Override
	public Patient selectById(Integer id) {
		return patientMapper.selectPatientById(id);
	}

	/**
	 * 修改患者信息
	 *
	 * @param patient
	 */
	@Override
	@Transactional
	public void update(Patient patient) {
		patientBasicInfoMapper.updateBasicInfo(patient);
		patientDetailInfoMapper.updateDetailInfo(patient);
		patientReturnVisitMapper.updateReturnVisit(patient);
	}


}


